//
//  UICityPicker.m
//  DDMates
//
//  Created by ShawnMa on 12/16/11.
//  Copyright (c) 2011 TelenavSoftware, Inc. All rights reserved.
//

#import "TSLocateView.h"
#import "SBJson.h"

#define kDuration 0.3

@interface TSLocateView()

@end

@implementation TSLocateView

- (id)initWithTitle:(NSString *)title delegate:(id)delegate isVillage:(BOOL)isVillage {
    self = [super init];
    if (self) {
        self.delegate = delegate;
        self.isVillage = isVillage;
        self.titleLabel.text = title;
        self.locatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 44, kScreenWidth, 216)];
        self.locatePicker.backgroundColor = [UIColor whiteColor];
        self.locatePicker.showsSelectionIndicator = YES;
        [self addSubview:self.locatePicker];
        self.locatePicker.dataSource = self;
        self.locatePicker.delegate = self;
        
        self.locate = [TSLocation sharedInstance];
        [self.locate refreshLocateInfo];
    }
    return self;
}

#pragma mark - PickerView lifecycle
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.isVillage ? 1 : 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (self.isVillage) {
        return self.locate.villages.count;
    } else {
        switch (component) {
            case 0:
                return self.locate.provinces.count;
            case 1:
                return self.locate.cities.count;
            case 2:
                return self.locate.towns.count;
            case 3:
                return self.locate.villages.count;
            default:
                return 0;
        }
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (self.isVillage) {
        return [self.locate.villages objectAtIndex:row];
    } else {
        switch (component) {
            case 0:
                return [[self.locate.provinces objectAtIndex:row] objectForKey:@"name"];
            case 1:
                return [[self.locate.cities objectAtIndex:row] objectForKey:@"name"];
            case 2:
                return [[self.locate.towns objectAtIndex:row] objectForKey:@"name"];
            case 3:
                return [self.locate.villages objectAtIndex:row];
            default:
                return nil;
        }
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (self.isVillage) {
        self.locate.selectForthRow = row;
    } else {
        if (component == 0) {
            self.locate.selectFirstRow = row;
            self.locate.selectSecondRow = 0;
            self.locate.selectThirdRow = 0;
            self.locate.selectForthRow = 0;
        } else if (component == 1) {
            self.locate.selectSecondRow = row;
            self.locate.selectThirdRow = 0;
        } else if (component == 2) {
            self.locate.selectThirdRow = row;
            self.locate.selectForthRow = 0;
        } else if (component == 3) {
            self.locate.selectForthRow = row;
        }
        self.locate.cities = [self.locate getArrayByComponent:1];
        self.locate.towns = [self.locate getArrayByComponent:2];
        self.locate.villages = [self.locate getArrayByComponent:3];
    }
    [pickerView reloadAllComponents];
    [self.locate refreshLocateInfo];
}

#pragma mark - Button lifecycle
- (IBAction)cancel:(id)sender {
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"TSLocateView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
        [self.delegate actionSheet:self clickedButtonAtIndex:0];
    }
}

- (IBAction)locate:(id)sender {
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"TSLocateView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
        [self.delegate actionSheet:self clickedButtonAtIndex:1];
    }
    
}

@end
